#include<bits/stdc++.h>
#define i64 long long
#define ull unsigned long long
#define fi first
#define se second
using namespace std;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;

void solve()
{
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; i++) cin >> a[i].fi;
    for (int i = 0; i < n; i++) cin >> a[i].se;

    auto check = [&](int mid)
        {
            multiset<pair<int, int>> s;
            for (int i = 0; i < n; i++) {
                if (s.size() == 0 || s.begin()->se == a[i].se) {
                    s.insert(a[i]);
                } else {
                    auto it = s.lower_bound({ mid - a[i].fi, 0 });
                    if (it == s.end()) return false;
                    s.erase(it);
                }
            }
            return true;
        };

    int l = 2, r = 1e9;
    while (l < r) {
        int mid = (l + 1 + r) / 2;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    cout << l << "\n";
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}